<template>


  <a-card :bordered="false">
    <div class="table-page-search-wrapper">

    </div>

    <div class="table-operator">
      <a-row>
        <a-col :span="14">
          <a-button type="primary" icon="plus" @click="$router.push('/landResourceManage/landResourceManageAdd')">新建</a-button>
        </a-col>
        <a-col :span="10" class="text-align-right">
          <a-input-search
            placeholder="请输入关键字"
            @search="onSearch"
            enterButton="查询"
            size="large" />
        </a-col>
      </a-row>


    </div>

    <a-table
      ref="table"
      size="default"
      :columns="columns"
      :dataSource="tableData"
      :showAlertInfo="true"
      @onSelect="onChange"
    >
            <a slot="infoAction"  @click="handleDetail(record)" slot-scope="text,record,index">
                {{text}}
            </a>
            <span slot="stateAction" :style="text=='审核驳回'?{color:'red'}:text=='审核通过'?{color:'blue'}:''" slot-scope="text,record,index">
                {{text}}
            </span>
            <span slot="number" slot-scope="text,record,index">
                {{index + 1}}
            </span>
            <span slot="action" slot-scope="text, record">
              <a @click="handleVerify(record)"><a-icon type="check-circle" />审核</a>
                <a-divider type="vertical"/>
              <a @click="handleEdit(record)"><a-icon type="edit" />编辑</a>
                <a-divider type="vertical"/>
              <a  style="color: red;">
                   <a-popconfirm title="确定删除吗？" @confirm="handleDelete(record)" okText="确定" cancelText="取消">
                           <a-icon type="delete" />删除
                    </a-popconfirm>
              </a>

            </span>
    </a-table>

    <a-modal
      title="编辑"
      width="70%"
      v-model="visible"
      @ok="saveFormData"
    >
      <landFormTemplate :landResourceFormDataJson = "landResourceFormData" @fatherEditMethod="getlandResourceList" ref="landForm"></landFormTemplate>
    </a-modal>
    <a-modal
      title="审核"
      width="70%"
      v-model="verifyBool"
    >
      <landResourceInfo :landResourceFormDataJson = "landResourceFormData" ref="landFormInfo"></landResourceInfo>
      <template slot="footer" class="text-align-center">
        <a-button   @click="verifyBool=false">搁置</a-button>
        <a-button  type="danger"  @click="verifyBack('审核驳回')"> 审核驳回</a-button>
        <a-button  type="primary"  @click="verifyBack('审核通过')"> 审核通过</a-button>
      </template>
    </a-modal>

    <a-modal
      title="详情"
      width="70%"
      v-model="detailBool"
      @ok="detailBool=false"
    >
      <landResourceInfo :landResourceFormDataJson = "landResourceFormData" ></landResourceInfo>
    </a-modal>

  </a-card>

</template>

<script>
  import STable from '@/components/table/'
  import ATextarea from "ant-design-vue/es/input/TextArea"
  import AInput from "ant-design-vue/es/input/Input"
  import moment from "moment"
  import {getRoleList, getServiceList} from '@/api/manage'
  import {getBase64} from '../../utils/util'
  import indexedDB from '../../db/db'
  import {landResource} from "../../db/landResource"
  import landFormTemplate from './land-form-template'
  import landResourceInfo from './land-resource-info'
  export default {
    name: "landResourceList",
    components: {
      AInput,
      ATextarea,
      STable,
      landFormTemplate,
      landResourceInfo
    },
    data() {
      return {
        detailBool:false,
        verifyBool:false,
        visible: false,
        searchText:"",
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 12},
        },
        landResourceFormData:{
          landId:"",
          licenseNum:"",
          licenseType:"",
          imgMap:"",
          landUser:"",
          mapNum:"",
          landTypeUse:"",
          useEndTime:"",
          sendBranch:"",
          useArea:"",
          duYouArea:"",
          fenTanArea:"",
          createDate:"",
          createPeople:"",
          landType:"",
          wordUrl:"",
          state:"待审核",
        },
        mdl: {},

        // 高级搜索 展开/关闭
        advanced: true,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: 'number',
            scopedSlots: {customRender: 'number'},
          },
          {
            title: '土地Id',
            dataIndex: 'landId',
            scopedSlots: {customRender: 'infoAction'},
          },
          {
            title: '证照类别',
            dataIndex: 'licenseType',
          },
          {
            title: '创建时间',
            dataIndex: 'createDate',
          },
          {
            title: '状态',
            dataIndex: 'state',
            scopedSlots: {customRender: 'stateAction'},
          },
          {
            title: '发布人',
            dataIndex: 'createPeople',
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '200px',
            scopedSlots: {customRender: 'action'},
          },
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return getServiceList(Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
        },

        selectedRowKeys: [],
        selectedRows: [],
        //数据库
        tableData: [],
      }
    },
    created() {
      getRoleList({t: new Date()})
    },
    mounted() {
      indexedDB.initDB()
      indexedDB.batInit('landResource', landResource);// 初始化数据
      this.getlandResourceList()
    },
    methods: {
      getlandResourceList() {
        let that = this
        this.visible = false;
        this.searchText = "";
        indexedDB.handleDataByCursor('landResource').then(res => {

          if (res.length > 0) {
            that.tableData = res
              that.tableData.forEach(item=>{
                  item.files.forEach(subItem=>{
                      subItem.url = URL.createObjectURL(subItem.originFileObj)
                  })
              })
            console.log(that.tableData)
          }else{
            that.tableData = []
          }
        })
      },
      onSearch(e){
        console.log(e)
        this.searchText = e;
        // this.getlandResourceList()
        let that = this
        indexedDB.handleDataByIndex('landResource', this.searchText,'landId').then(res => {

          if (res.length > 0) {
            that.tableData = res
            console.log(that.tableData)
          }else{
            that.tableData = []
          }
        })
      },
      handleEdit(record) {
        this.visible = true
        this.$nextTick(()=>{
          this.$refs.landForm.handleEdit(record);
        })
      },
      handleVerify(record){
        this.landResourceFormData = Object.assign({}, record)
        this.verifyBool = true;
      },
      handleDetail(record){
        this.landResourceFormData = Object.assign({}, record)
        this.detailBool = true;
      },
      handleDelete(record) {
        indexedDB.delete({name:"landResource"},record.id).then((res)=>{
          if(res==true){
            this.$message.info("删除成功！");
            this.getlandResourceList()
          }else{
            this.$message.error("删除失败！");
          }
        })
      },
      onChange(row) {
        this.selectedRowKeys = row.selectedRowKeys
        this.selectedRows = row.selectedRows

        console.log(this.$refs.table)
      },
      toggleAdvanced() {
        this.advanced = !this.advanced
      },

      resetSearchForm() {
        this.queryParam = {
          date: moment(new Date()),
        }
      },

      saveFormData(){
        this.$refs.landForm.editeSaveFormData();

      },
      verifyBack(state){
        let _this = this
        console.log(_this.landResourceFormData)
        _this.landResourceFormData.state = state;
            indexedDB.update({name:"landResource"},_this.landResourceFormData).then((res)=>{
              console.log(res);
              if(res==true){
                _this.$message.info(state+"成功!");
                _this.landResourceFormData = {
                  landId:"",
                  licenseNum:"",
                  licenseType:"",
                  imgMap:"",
                  landUser:"",
                  mapNum:"",
                  landTypeUse:"",
                  useEndTime:"",
                  sendBranch:"",
                  useArea:"",
                  duYouArea:"",
                  fenTanArea:"",
                  createDate:"",
                  createPeople:"",
                  landType:"",
                  wordUrl:"",
                  state:"待审核",
                };
                _this.getlandResourceList();
                _this.verifyBool = false;
              }else{
                _this.$message.error(state+"失败！");
              }
        })
      },



    },
    watch: {
      /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
    },
  }
</script>